prepare("insert into pins (pin,seria) values (:a,:b)"); $sql->bindparam(":a", $newpin) ; $sql->bindparam(":b", $sm) ; if($sql->execute()) $c = $c + 1 ; } $message = " Uploaded Succesfully ". $c ." Pins"; } // Check if form is submitted /*if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) { // Debug: Check if we're reaching this point // echo "Form submitted!
"; // Check if file was uploaded if (!isset($_FILES['file']) || $_FILES['file']['error'] === UPLOAD_ERR_NO_FILE) { $error = 'Please select a CSV file to upload.'; } elseif ($_FILES['file']['error'] !== UPLOAD_ERR_OK) { // Handle specific upload errors switch ($_FILES['file']['error']) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $error = 'File size exceeds limit.'; break; case UPLOAD_ERR_PARTIAL: $error = 'File was only partially uploaded.'; break; case UPLOAD_ERR_NO_TMP_DIR: $error = 'Missing temporary folder.'; break; case UPLOAD_ERR_CANT_WRITE: $error = 'Failed to write file to disk.'; break; case UPLOAD_ERR_EXTENSION: $error = 'File upload stopped by extension.'; break; default: $error = 'Unknown upload error.'; } } else { // Get file information $fileName = $_FILES['file']['name']; $fileTmpName = $_FILES['file']['tmp_name']; $fileSize = $_FILES['file']['size']; // Debug: Show file info // echo "File: $fileName
"; // echo "Temp: $fileTmpName
"; // echo "Size: $fileSize
"; // Validate file extension $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if ($fileExt !== 'csv') { $error = 'Invalid file type. Please upload a CSV file (.csv extension).'; } elseif ($fileSize > 5 * 1024 * 1024) { // 5MB limit $error = 'File size exceeds 5MB limit. Please upload a smaller file.'; } elseif (!is_uploaded_file($fileTmpName)) { $error = 'Invalid file upload attempt.'; } else { try { // Check if file is readable if (!is_readable($fileTmpName)) { $error = 'Cannot read the uploaded file.'; } else { // Open and read the CSV file $handle = fopen($fileTmpName, "r"); if ($handle === false) { $error = 'Failed to open the uploaded file.'; } else { $seria = 1; $recordsProcessed = 0; $duplicateCount = 0; $invalidCount = 0; // First, let's count total rows for feedback $totalRows = 0; while (($data = fgetcsv($handle, 1000, ",")) !== false) { $totalRows++; } // Reset file pointer rewind($handle); // Start transaction for better performance $DBcon->beginTransaction(); // Prepare the insert statement $stmt = $DBcon->prepare("INSERT INTO pins (pin, seria) VALUES (:pin, :seria)"); // Check if table exists and has correct structure try { $checkTable = $DBcon->query("SELECT 1 FROM pins LIMIT 1"); } catch (PDOException $e) { // Table might not exist or have different structure // Let's create it if it doesn't exist $createTable = $DBcon->exec(" CREATE TABLE IF NOT EXISTS pins ( id INT AUTO_INCREMENT PRIMARY KEY, pin VARCHAR(255) NOT NULL, seria INT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) "); } $rowCount = 0; while (($data = fgetcsv($handle, 1000, ",")) !== false) { $rowCount++; // Skip empty rows if (empty($data[0])) { continue; } $pin = trim($data[0]); // Validate PIN format if (!empty($pin)) { // Basic validation - adjust as needed if (strlen($pin) < 4 || strlen($pin) > 20) { $invalidCount++; continue; } // Check for duplicates (optional) $checkDuplicate = $DBcon->prepare("SELECT id FROM pins WHERE pin = :pin"); $checkDuplicate->execute([':pin' => $pin]); if ($checkDuplicate->fetch()) { $duplicateCount++; continue; } // Insert the PIN try { $stmt->execute([ ':pin' => $pin, ':seria' => $seria ]); if ($stmt->rowCount() > 0) { $recordsProcessed++; $seria++; } } catch (PDOException $e) { // Log error but continue error_log("Error inserting PIN '{$pin}': " . $e->getMessage()); $invalidCount++; } } } fclose($handle); // Commit transaction $DBcon->commit(); $totalRecords = $recordsProcessed; // Build result message if ($recordsProcessed > 0) { $message = "✅ Successfully uploaded {$recordsProcessed} PINs."; // Add additional info $additionalInfo = []; if ($duplicateCount > 0) { $additionalInfo[] = "⚠️ {$duplicateCount} duplicates skipped"; } if ($invalidCount > 0) { $additionalInfo[] = "⚠️ {$invalidCount} invalid entries skipped"; } if (!empty($additionalInfo)) { $message .= "
" . implode(" • ", $additionalInfo) . ""; } } else { if ($totalRows > 0) { if ($duplicateCount > 0 && $duplicateCount === $totalRows) { $error = "All {$duplicateCount} PINs in the file already exist in the database."; } elseif ($invalidCount > 0 && $invalidCount === $totalRows) { $error = "All {$invalidCount} PINs in the file are invalid (must be 4-20 characters)."; } else { $error = "No valid PINs were uploaded from the file."; } } else { $error = "The CSV file appears to be empty or contains no valid data."; } } } } } catch (Exception $e) { // Rollback transaction on error if ($DBcon->inTransaction()) { $DBcon->rollBack(); } $error = "Upload error: " . htmlspecialchars($e->getMessage()); error_log("PIN Upload Error: " . $e->getMessage()); // Debug output // echo "Error: " . $e->getMessage() . "
"; } } } // Debug: Show what we got // echo "Message: $message
"; // echo "Error: $error
"; } */ ?> PIN Management System | Upload

CARD PIN UPLOAD SYSTEM

Upload PINs from CSV file

The CSV file should contain PIN codes in the first column, one per line.
query("SELECT COUNT(*) as total FROM pins"); $totalPins = $countStmt->fetch()['total']; } catch (PDOException $e) { $totalPins = 0; } ?>